﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;

namespace FYPHP
{
    public partial class VirtualShop1 : System.Web.UI.Page
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["fyphp"].ConnectionString);
        
        protected void Page_Load(object sender, EventArgs e)
        {
            SqlCommand cmdSearch;
            SqlDataReader dtrSearch;
            conn.Open();

            cmdSearch = new SqlCommand("Select layout From Virtual_Shop Where v_id=@id", conn);
            cmdSearch.Parameters.AddWithValue("@id", 1);
            dtrSearch = cmdSearch.ExecuteReader();

            dtrSearch.Read();

            String s = dtrSearch["layout"].ToString();
            
            dtrSearch.Close();

            var control = Page.LoadControl("~/layout"+s+".ascx");
            PlaceHolder1.Controls.Add(control);

            conn.Close();
        }

        protected void btnChange_Click(object sender, EventArgs e)
        {
            SqlCommand cmdSearch, cmdIn;
            SqlDataReader dtrSearch;
            conn.Open();

            cmdSearch = new SqlCommand("Select layout From Virtual_Shop Where u_id=@id", conn);
            cmdSearch.Parameters.AddWithValue("@id", 1);
            dtrSearch = cmdSearch.ExecuteReader();

            dtrSearch.Read();
            int s = Convert.ToInt32(dtrSearch["layout"]);
            int x = 0;

            if (s == 1)
                x = 2;
            else
                x = 1;

            dtrSearch.Close();

            cmdIn = new SqlCommand("UPDATE Virtual_Shop SET layout=@l WHERE u_id=@id", conn);
            cmdIn.Parameters.AddWithValue("@l", x);
            cmdIn.Parameters.AddWithValue("@id", 1);
            cmdIn.ExecuteNonQuery();

            Redirect("VirtualShop.aspx");

            conn.Close();
        }

        private void Redirect(string msg)
        {
            Label lbl = new Label();
            lbl.Text = "<script language=\"javascript\">window.location='" + msg + "';</script>";
            Page.Controls.Add(lbl);
        }
    }
}